//
//  CalendarViewController.m
//  pocketNews
//
//  Created by DS on 2018/10/31.
//  Copyright © 2018年 徐双. All rights reserved.
//

#import "CalendarViewController.h"
#import "LTSCalendarView.h"

#import "CalNewsTableViewCell.h"

#import "HomeSonModel.h"

#import "WBWebViewController.h"

@interface CalendarViewController ()<LTSCalendarViewDelegate,LTSCalendarEventSource,UITableViewDataSource,UITableViewDelegate>
{
    NSMutableDictionary *eventsByDate;
}
@property (weak, nonatomic) IBOutlet UILabel *label;

@property (nonatomic,strong)LTSCalendarView *calendarView;

Strong UITableView * tableView;


@end



@implementation CalendarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"日历";

    UIButton *btn1 =[[UIButton alloc]initWithFrame:CGRectMake(0,0, 22, 22)];
    
    [btn1 addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    [btn1 setBackgroundImage:[[UIImage imageNamed:@"fanhui"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithCustomView:btn1];
    self.navigationItem.leftBarButtonItem  = item;
 
    self.automaticallyAdjustsScrollViewInsets = NO;
    LTSCalendarView *calendarView = [[LTSCalendarView alloc]initWithFrame:CGRectMake(0, 200, 375, 667-200)];
    //calendarView.backgroundColor = getColor(whiteColor);
    calendarView.delegate = self;
    [self.view addSubview:calendarView];
    ///设置 日历外观 样式
    //    calendarView.calendar.calendarAppearance.isShowLunarCalender = YES;
    self.calendarView = calendarView;
    calendarView.calendar.calendarAppearance.weekDayHeight = 40;
    calendarView.calendar.calendarAppearance.weekDayTextFont = [UIFont systemFontOfSize:14];
    calendarView.calendar.currentDateSelected = [NSDate date];
    [self.calendarView.calendar reloadAppearance];
    //添加事件
    [self createRandomEvents];
    
 
}

-(void)onClick:(UIButton *)btn{
    [self.navigationController popViewControllerAnimated:YES];
    
}

#pragma maek -- LTSCalendarViewDelegate --
- (void)calendarView:(LTSCalendarView *)calendarView didSelectedIndexPath:(NSIndexPath *)indexPath{
    
    NSLog(@"%@",indexPath);
}
- (void)calendarDidLoadPage:(LTSCalendarManager *)calendar{
    
}

#pragma mark -- LTSCalendarEventSource --
// 该日期是否有事件
- (BOOL)calendarHaveEvent:(LTSCalendarManager *)calendar date:(NSDate *)date
{
    NSString *key = [[self dateFormatter] stringFromDate:date];
    
    if(eventsByDate[key] && [eventsByDate[key] count] > 0){
        return YES;
    }
    //
    return NO;
}
//当前 选中的日期  执行的方法
- (void)calendarDidDateSelected:(LTSCalendarManager *)calendar date:(NSDate *)date
{
    
    
    
    NSString *key = [[self dateFormatter] stringFromDate:date];
    self.label.text =  key;
    NSArray *events = eventsByDate[key];
    //    self.title = key;
    
    if (events.count>0) {
        
        //该日期有事件    tableView 加载数据
    }
    
}


//tabelView
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - UNIT_HEIGHT(40) - height20) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.sectionFooterHeight = .01;
        _tableView.sectionHeaderHeight = .01;
        _tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0.01)];
        _tableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0.01)];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        //_tableView.backgroundColor = getColor(whiteColor);
    }
    return _tableView;
}
#pragma mark - <UITableViewDelegate , UITableViewDataSource>

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CalNewsTableViewCell * cell = [CalNewsTableViewCell cellForTableView:tableView];
    
    cell.model = self.dataArr[indexPath.row];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    return UNIT_HEIGHT(110);
    
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    
    return @"今日推荐";
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return UNIT_HEIGHT(30);
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HomeSonModel * model = self.dataArr[indexPath.row];


    WBWebViewController *webVC = [[WBWebViewController alloc] init];
    webVC.urlString = model.url;
    webVC.isWKWebView = YES;
    //webVC.title = @"2345";

    webVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:webVC animated:YES];
    
    
}






- (IBAction)isShowLunar:(id)sender {
    self.calendarView.calendar.calendarAppearance.isShowLunarCalender = !self.calendarView.calendar.calendarAppearance.isShowLunarCalender;
    //重新加载外观
    
    [self.calendarView.calendar reloadAppearance];
    
}
- (IBAction)nextMonth:(id)sender {
    [self.calendarView.calendar loadNextPage];
}

- (IBAction)previousMonth:(id)sender {
    [self.calendarView.calendar loadPreviousPage];
}
- (IBAction)monday:(id)sender {
    self.calendarView.calendar.calendarAppearance.firstWeekday = 2;
    [self.calendarView.calendar reloadAppearance];
}
- (IBAction)sunday:(id)sender {
    self.calendarView.calendar.calendarAppearance.firstWeekday = 1;
    
    [self.calendarView.calendar reloadAppearance];
}
- (IBAction)full:(id)sender {
    self.calendarView.calendar.calendarAppearance.weekDayFormat = LTSCalendarWeekDayFormatFull;
    [self.calendarView.calendar reloadAppearance];
}
- (IBAction)fullShort:(id)sender {
    self.calendarView.calendar.calendarAppearance.weekDayFormat = LTSCalendarWeekDayFormatShort;
    [self.calendarView.calendar reloadAppearance];
}
- (IBAction)single:(id)sender {
    self.calendarView.calendar.calendarAppearance.weekDayFormat = LTSCalendarWeekDayFormatSingle;
    [self.calendarView.calendar reloadAppearance];
}

- (void)createRandomEvents
{
    eventsByDate = [NSMutableDictionary new];
    
    for(int i = 0; i < 30; ++i){
        // Generate 30 random dates between now and 60 days later
        NSDate *randomDate = [NSDate dateWithTimeInterval:(rand() % (3600 * 24 * 60)) sinceDate:[NSDate date]];
        
        // Use the date as key for eventsByDate
        NSString *key = [[self dateFormatter] stringFromDate:randomDate];
        
        if(!eventsByDate[key]){
            eventsByDate[key] = [NSMutableArray new];
        }
        
        [eventsByDate[key] addObject:randomDate];
    }
    
}

- (NSDateFormatter *)dateFormatter
{
    static NSDateFormatter *dateFormatter;
    if(!dateFormatter){
        dateFormatter = [NSDateFormatter new];
        dateFormatter.dateFormat = @"yyyy.MM.dd";
    }
    
    return dateFormatter;
}



@end
